<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <%@ include file="/static/admin/jsp/include.jsp" %>

    <script type="text/javascript">
        var grid;
        var index;

        var addFun = function ($dialog, $grid, $pjq) {
            var dialog = parent.sy.modalDialog({
                title: '新增',
                width: 800,
                height: 600,
                url: sy.contextPath + '/go?path=goods/suggest/suggestEdit',
                buttons: [{
                    text: '保存',
                    handler: function () {
                        dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
                    }
                }]
            });
        };

        /**
         * 删除今日推荐--修改isToday字段
         */
        var delFun = function () {
            var rows = grid.datagrid('getSelections');
            if (rows.length == 0) {
                parent.$.messager.w('请选择需要删除的记录！');
                return;
            }
            parent.$.messager.confirm('询问', '您确定要删除此记录？', function (r) {
                if (r) {
                    var ids = [];
                    for (var i = 0, l = rows.length; i < l; i++) {
                        var r = rows[i];
                        ids.push(r.id);
                    }
                    var id = ids.join(',');
                    $.post(sy.contextPath + '/goodsSuggest/del', {
                        id: id
                    }, function () {
                        rows.length = 0;//必须，否则有bug
                        grid.datagrid('reload');
                    }, 'json');
                }
            });
        };

        $(function () {
            grid = $('#grid').datagrid({
                url: sy.contextPath + '/goodsSuggest/list',
                columns: [[{
                    width: $(this).width() * 0.1,
                    title: '作品名称',
                    field: 'goodsName',
                    align: 'center',
                }, {
                    width: $(this).width() * 0.1,
                    title: '图片',
                    field: 'img',
                    align: 'center',
                    formatter: function (value, row, index) {
                        if (value != null && value != '') {
                            return po.showImg(value, 20, 20);
                        } else {
                            return null;
                        }
                    }
                },{
                    width: $(this).width() * 0.1,
                    title: '设置为今日推荐',
                    field: 'state',
                    align: 'center',
                    formatter: function (value, row, index) {
                        if(value == 1){
                            return '<a href="javascript:void(0);" onclick="setIsToday(\''+row.id+'\',\''+0+'\')" class="button button-info" title="取消">取消</a>';
                        }else{
                            return '<a href="javascript:void(0);" onclick="setIsToday(\''+row.id+'\',\''+1+'\')" class="button button-info" title="设置为今日推荐">设置为今日推荐</a>';
                        }

                    }
                }, {
                    width: $(this).width() * 0.1,
                    title: '详情',
                    field: 'detail',
                    align: 'center',
                    formatter: function (v,r,i) {
                        if(r.state == 1){
                            return '<a href="javascript:void(0);" onclick="detail(\''+r.id+'\')" class="button button-info" title="查看详情">查看详情</a>';
                        }

                    }
                }
                ]]
            });


            $('#type').combobox({
                onSelect: function (rec) {
                    grid.datagrid('load', sy.serializeObject($('#searchForm')));
                }
            });
        });

        function detail (id) {
            var url = sy.contextPath + '/go?path=/goods/suggest/suggestInfo&id='+id;
            var dialog = parent.sy.modalDialog({
                title : '详情',
                width : 800,
                height : 700,
                url : url,
                buttons : [ {
                    text : '保存',
                    handler : function() {
                        dialog.find('iframe').get(0).contentWindow.submitForm(dialog, grid, parent.$);
                    }
                } ]
            });
        }

        function setIsToday(id,param){
            var url = sy.contextPath + '/goodsSuggest/check';
            if(param != 0){
                $.post(url, "", function (result) {
                    if(result.code == 1){
                        var data = {id : id, state : param}
                        SaveData(data)
                    }else{
                        return layer.msg(result.msg)
                    }
                    grid.datagrid('reload');
                }, 'json');
            }else{
                var data = {id : id, state : param}
                SaveData(data)
            }



        }

        function SaveData(data) {
            var url = sy.contextPath + '/goodsSuggest/save';
            $.post(url, data, function () {
                grid.datagrid('reload');
            }, 'json');
        }
    </script>
</head>
<body>
<div id="toolbar">
    <form id="searchForm">
        <div>
            <input type="text" class="easyui-textbox" name="goodsName" style="width: 150px" prompt="作品名称"/>
            <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa-search'"
               onclick="grid.datagrid('load',sy.serializeObject($('#searchForm')));">过滤</a>
            <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa-search-minus'"
               onclick="$('#searchForm input').val('');grid.datagrid('load',{});">重置过滤</a>
        </div>
    </form>
    <div class="tbbutton">

        <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa-plus-circle',plain:true"
           onclick="addFun();">添加</a>
        <%--<a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa-pencil',plain:true" onclick="editFun();">修改</a>--%>
        <a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'fa-trash',plain:true"
           onclick="delFun();">删除</a>

    </div>
</div>
<table id="grid" data-options="fit:true,border:false"></table>
</body>
</html>